package com.example.administrator.szb.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.bean.ResultBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.Contants;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.GetJsonDataUtil;
import com.example.administrator.szb.util.Toasts;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by D N on 2017/10/10.
 */

public class XGMMActivity extends BaseActivity {
    @Bind(R.id.xgmm_image_fh)
    ImageView xgmmImageFh;
    @Bind(R.id.xgmm_edit_sjh)
    EditText xgmmEditSjh;
    @Bind(R.id.xgmm_edit_jmm)
    EditText xgmmEditJmm;
    @Bind(R.id.xgmm_edit_xmm)
    EditText xgmmEditXmm;
    @Bind(R.id.xgmm_edit_qrxmm)
    EditText xgmmEditQrxmm;
    @Bind(R.id.xgmm_button_qrxg)
    Button xgmmButtonQrxg;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xgmm_activity);
        SampleApplicationLike.getActivitiesInstance().add(this);
        ButterKnife.bind(this);

    }

    @OnClick({R.id.xgmm_image_fh, R.id.xgmm_edit_sjh, R.id.xgmm_edit_jmm, R.id.xgmm_edit_xmm, R.id.xgmm_edit_qrxmm, R.id.xgmm_button_qrxg})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.xgmm_image_fh:
                finish();
                break;
            case R.id.xgmm_edit_sjh:
                break;
            case R.id.xgmm_edit_jmm:
                break;
            case R.id.xgmm_edit_xmm:
                break;
            case R.id.xgmm_edit_qrxmm:
                break;
            case R.id.xgmm_button_qrxg:
                requestXG();
                break;
        }
    }

    private void requestXG() {
        boolean flag = checkInfo();
        if (flag){
            doXGMM();
        }
    }

    private void doXGMM() {
        HashMap<String,Object> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("pass_left",xgmmEditJmm.getText().toString());
        map.put("pass_right",xgmmEditXmm.getText().toString());
        map.put("users_id",SampleApplicationLike.getUserloginInstance().getId()+"");
        String json = GetJsonDataUtil.simpleMapToJsonStr(map);
        String url = URLAddress.URLBASE + URLAddress.URL_XGMM;

        HttpUtils.post2(activity,url, map, 0, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                ResultBean bean = JSON.parseObject(str,new TypeReference<ResultBean>(){});
                if (bean.getResult() == 1){
                    Toasts.show(XGMMActivity.this, "修改成功", Toast.LENGTH_SHORT);
                    finish();
                }else{
                    Toasts.show(XGMMActivity.this, ""+bean.getErr_msg(), Toast.LENGTH_SHORT);
                }
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });

    }

    private boolean checkInfo() {
        if (TextUtils.isEmpty(xgmmEditJmm.getText().toString())) {
            Toasts.show(this, "请输入旧密码", Toast.LENGTH_SHORT);
        }else if (TextUtils.isEmpty(xgmmEditXmm.getText().toString())){
            Toasts.show(this, "请输入新密码", Toast.LENGTH_SHORT);
        }else if(!Contants.passIsRight(xgmmEditXmm.getText().toString())){
            DialogUtil.showSimpleDialog(this,"密码必须是6~20位的且有字母或数字的组合");
        }else if (TextUtils.isEmpty(xgmmEditQrxmm.getText().toString())){
            Toasts.show(this, "请输入确认密码", Toast.LENGTH_SHORT);
        }else if (!xgmmEditXmm.getText().toString().trim().equals(xgmmEditQrxmm.getText().toString().trim())){
            Toasts.show(this, "两次密码不一致", Toast.LENGTH_SHORT);
        }else{
            return true;
        }
        return false;
    }
}
